import set from "../apis/api";
import {set_GyToken, set_wxuser} from "../actions/IndexAction";
import Taro, {Component} from '@tarojs/taro'

const base = function (obj) {

  obj.wx_checkSession = () => {
    return new Promise(function (resolve, reject) {
      wx.checkSession({
        success: function (res) {
          resolve(res)
        },
        fail: function (res) {
          reject(res);
        }
      })
    })
  }
  obj.hideall = () => {
    wx.hideLoading();
    wx.stopPullDownRefresh();
  }
  obj.showtoast = (title) => {
    wx.showToast({
      title: title,
      icon: 'none',
    })
  }

  obj.getGyToken = () => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: set.getGyToken,
        data: {},
        method: 'GET',
        header: {'content-type': 'application/json'},
        success: function (res) {
          resolve(res)
        },
        fail(res) {
          reject(res)
        }
      });
    })
  }

  obj.wx_request = (url, data = {}, method = 'GET', header = {'content-type': 'application/json'}) => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: data,
        method: method,
        header: header,
        success: function (res) {
          if (res.data.status == 109) {

            obj.checkGyToken();

            resolve(res)

          }
          if (res.data.status != 100) {
            reject(res)
          }
          resolve(res)
        },
        fail(res) {
          reject(res)
        }
      });
    })
  }
  obj.error = (e) => {
    wx.hideLoading();
    obj.setState({
      wxerror: true
    })
  }
  obj.getopenid = (res) => {
    return new Promise((resolve, reject) => {
      console.log(res.code)
      obj.wx_request(set.getopenid, {
        access_token: obj.props.gytoken.gytoken,
        code: res.code,
        wxid: 2
      }, "POST", {'Content-Type': 'application/x-www-form-urlencoded'}).then((data) => {
        console.log("己获取到openid", data.data.data)
        resolve(data.data.data);
      }).catch((e) => {
        console.log(e)
        obj.error(e)
      })
    })

  }
  obj.setwxuser = (wxuser) => {
    wxuser = Object.assign({}, obj.props.wxuser.wxuser, wxuser);
    obj.props.dispatch(set_wxuser(wxuser))
    wx.setStorageSync("wxuser", JSON.stringify(wxuser))
  }


  obj.getwxuser = () => {
    var wxuser = wx.getStorageSync("wxuser");
    if (wxuser) {
      wxuser = JSON.parse(wxuser);
    }
    obj.props.dispatch(set_wxuser(wxuser))
    return wxuser
  }

  obj.removewxuser = () => {
    var wxuser = {};
    obj.props.dispatch(set_wxuser(wxuser))
    wx.removeStorageSync("wxuser")
  }
  obj.setGyToken = (gytoken) => {
    console.log("aaaa", gytoken);
    obj.props.dispatch(set_GyToken(gytoken))
    wx.setStorageSync("gytoken", gytoken)
  }
  obj.getCacheGyToken = () => {
    var gytoken = wx.getStorageSync("gytoken");
    obj.props.dispatch(set_GyToken(gytoken))
    return gytoken
  }
  obj.checkGyToken = () => {
    var token = obj.getCacheGyToken();
    return new Promise((resolve, reject) => {
      wx.request({
        url: set.checkToken,
        data: {
          access_token: token,
          wxid: 2
        },
        method: "POST",
        header: {'Content-Type': 'application/x-www-form-urlencoded'},
        success: function (res) {
          if (res.data.status == 109) {
            obj.getGyToken().then((gytoken)=>{
              obj.setGyToken(gytoken.data.data)
              obj.checkwxuser()
              resolve(res)
            })
          }else{

            obj.checkTokenLogin().then((res)=>{
              resolve(res)
            })
          }
          resolve(res)
        },
        fail(res) {
          reject(res)
        }
      });
    })
  }

  obj.checkTokenLogin = () => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: set.get_login,
        data: {
          access_token: obj.props.gytoken.gytoken,
          wxid: 2
        },
        method: "POST",
        header: {'Content-Type': 'application/x-www-form-urlencoded'},
        success: function (res) {
          if (res.data.status != 100) {
            obj.checkwxuser()
          }
          resolve(res)
        },
        fail(res) {
          reject(res)
        }
      });
    })
  }


  obj.checkwxuser = () => {
    var wxuser = {};
    obj.wx_login().then((res) => {
        obj.getopenid(res).then((openid) => {
          wxuser = Object.assign({}, wxuser, openid)
          obj.setwxuser(wxuser)
          wx.hideLoading();
          Taro.redirectTo({url: "/pages/gypage/login"})

        }).catch((e) => {
          obj.error(e)
        })


    }).catch((e)=>{
      console.log(e)
    })
  }
  obj.wx_login = () => {
    return new Promise(function (resolve, reject) {

      wx.login({
        success: function (res) {
          console.log("登陆成功")
          console.log(res);
          resolve(res)
        },
        fail: function (res) {
          console.log("登陆失败")
          reject(res);
        }
      })
    })
  }
  obj.chooseImage = () => {
    return new Promise(function (resolve, reject) {
      wx.chooseImage({
        success: function (res) {
          resolve(res)
        },
        fail: function (res) {
          reject(res);
        }
      })
    })
  }

  obj.checkLogin = () => {
    return new Promise((resolve, reject) => {
      if (obj.props.userinfo.userinfo) {
        resolve();
      } else {
        reject();
      }
    })

  }
  obj.date=(timestamp)=>{
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
  }

}
export default base